Cross-referencing und Code-Chunk-Optionen
Humboldt-Universität zu Berlin
2023-05-30
Go to menti.com and enter the code on the next screen
Last week you…
Dichte- und Histogrammdiagramme
Today we will…
tidyverseknitr (new)Shortcuts:
Cmd/Strg+Alt+I: insert new chunk
Cmd/Strg+Enter: run single line of code
Cmd/Strg+Shift+Enter: run whole code chunk
code chunks should be relatively self-contained
#| directly under ```{r}
#| label: simple-math will label the chunk ‘simple-math’- or _)Unique chunk labels
Chunk labels must always be unique within a script!
simple-mathAufgabe 1: Chunk labels
Beispiel 1
Cmd/Strg-Alt-I, and add some simple matheval: false prevents code from being printed in rendered outputinclude: false runs the code, but doesn’t show the code or results in the final documentecho: false prevents code, but not the output, from appearing in the rendered outputmessage: false or warning: false prevents messages or warnings from appearing in the rendered outputresults: hide hides printed output; fig-show: false hides plotserror: true renders the document even if errors are encounteredWill the following code chunk appear in the rendered output? Will the code be run?
Will the following code chunk appear in the rendered output? Will the code be run?
The following table summarizes which types of output each option suppresses:
| Option | Run code | Show code | Output | Plots | Messages | Warnings |
|---|---|---|---|---|---|---|
eval: false |
X | X | X | X | X | |
include: false |
X | X | X | X | X | |
echo: false |
X | |||||
results: hide |
X | |||||
fig-show: hide |
X | |||||
message: false |
X | |||||
warning: false |
X |
eval, echo, include, and message
excute:
Aufgabe 2: Chunk options
Beispiel 2
eval: false and echo: true
labels for code chunks that print a figure need to start with fig-
Abbildung 2: ?(caption)
fig-cap: adds a figure caption which will appear in the rendered document
fig-cap: "..."
Abbildung 3: Departure delay by arrival delay for December 2013. Airline is indicated via point colour.
fig-width: sets the width of the figure in inches (e.g., fig-width = 4)fig-height: sets the height of the figure in inches (e.g., fig-height = 4)fig-asp: sets the aspect-ratio of your figure (if you set only height or width; e.g., fig-asp = 0.618)out-width: sets the width of the figure in percentage to line width (e.g., out-width = "70%")out-height: sets the height of the figure in percentage to line width (e.g., out-height = "30%")fig-align: centre centres the figure on the output pagekable() function from the knitr package# A tibble: 6 × 5
year month day dep_time sched_dep_time
<dbl> <dbl> <dbl> <dbl> <dbl>
1 2013 1 1 517 515
2 2013 1 1 533 529
3 2013 1 1 542 540
4 2013 1 1 544 545
5 2013 1 1 554 600
6 2013 1 1 554 558
label and tbl-cap
| year | month | day | dep_time | sched_dep_time |
|---|---|---|---|---|
| 2013 | 1 | 1 | 517 | 515 |
| 2013 | 1 | 1 | 533 | 529 |
| 2013 | 1 | 1 | 542 | 540 |
| 2013 | 1 | 1 | 544 | 545 |
| 2013 | 1 | 1 | 554 | 600 |
| 2013 | 1 | 1 | 554 | 558 |
```{r}
#| output-location: fragment
#| label: tbl-flights2
#| tbl-cap: "A table made with `knitr`. The first 6 rows of the first 5 columns from the flights.csv dataset are printed."
df_flights %>%
select(1:5) %>%
head() %>%
knitr::kable(
col.names = c("Year", "Month", "Day", "Dep. Time", "Sched. Dep. Time")
)
```| Year | Month | Day | Dep. Time | Sched. Dep. Time |
|---|---|---|---|---|
| 2013 | 1 | 1 | 517 | 515 |
| 2013 | 1 | 1 | 533 | 529 |
| 2013 | 1 | 1 | 542 | 540 |
| 2013 | 1 | 1 | 544 | 545 |
| 2013 | 1 | 1 | 554 | 600 |
| 2013 | 1 | 1 | 554 | 558 |
@ followed by the label
@fig-flights-dec120.So the text:
@fig-flights-dec120-3shows the departure and arrive delays for December 2013.@fig-flights-dec120also shows this data, but doesn’t have a caption.@fig-flights-dec120-2also shows this data, and does have a caption, but is not sized.
Will print:
Abbildung 4 shows the departure and arrive delays for December 2013. Abbildung 2 also shows this data, but doesn’t have a caption. Abbildung 3 also shows this data, and does have a caption, but is not sized.
Create a copy of your report from last week, and:
execute and include: false
fig-out: 6 and fig-align: center
knitr::kable() to print tables of whichever summary you printed.
label and tbl-caption
labels and captions@. When you render the document, does it say ‘Abbildung 1’?Didn’t do a report? Then just copy the code from the solutions shared on Moodle.
Heute haben wir…
Hergestellt mit R version 4.3.0 (2023-04-21) (Already Tomorrow) und RStudioversion 2023.3.0.386 (Cherry Blossom).
R version 4.3.0 (2023-04-21)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.2.1
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: Europe/Berlin
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] knitr_1.42 patchwork_1.1.2 here_1.0.1 lubridate_1.9.2
[5] forcats_1.0.0 stringr_1.5.0 dplyr_1.1.2 purrr_1.0.1
[9] readr_2.1.4 tidyr_1.3.0 tibble_3.2.1 ggplot2_3.4.2
[13] tidyverse_2.0.0
loaded via a namespace (and not attached):
[1] utf8_1.2.3 generics_0.1.3 stringi_1.7.12 hms_1.1.3
[5] digest_0.6.31 magrittr_2.0.3 evaluate_0.21 grid_4.3.0
[9] timechange_0.2.0 fastmap_1.1.1 rprojroot_2.0.3 jsonlite_1.8.4
[13] fansi_1.0.4 scales_1.2.1 cli_3.6.1 rlang_1.1.1
[17] crayon_1.5.2 bit64_4.0.5 munsell_0.5.0 withr_2.5.0
[21] yaml_2.3.7 tools_4.3.0 parallel_4.3.0 tzdb_0.4.0
[25] colorspace_2.1-0 pacman_0.5.1 vctrs_0.6.2 R6_2.5.1
[29] lifecycle_1.0.3 bit_4.0.5 vroom_1.6.3 pkgconfig_2.0.3
[33] pillar_1.9.0 gtable_0.3.3 glue_1.6.2 xfun_0.39
[37] tidyselect_1.2.0 rstudioapi_0.14 farver_2.1.1 htmltools_0.5.5
[41] rmarkdown_2.21 labeling_0.4.2 compiler_4.3.0
Woche 7 - Quarto 2